/*
 * DiscoveryFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.page.active;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.data.recyclertype.TopArticle;
import com.yidiandao.business.ui.widget.recyclerview.LoadMoreRecyclerContainer;
import com.yidiandao.business.ui.widget.recyclerview.MultiTypeLoadMoreAdapter;
import com.yidiandao.business.ui.widget.recyclerview.provider.DiscoveryActivePageProvider;
import com.yidiandao.business.ui.widget.recyclerview.provider.DiscoveryNewsTopPageProvider;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 发现-活动-二级页面 (单个活动页面)
 */
public class DiscoveryActivePageFragment extends SuperBaseFragment implements DiscoveryActivePageContract.View {

    public static final String TAG = DiscoveryActivePageFragment.class.getSimpleName();

    private RecyclerView mRecyclerView;

    private PtrClassicFrameLayout mPtrFrame;

    private MultiTypeLoadMoreAdapter mAdapter;

    private DiscoveryActivePageContract.Presenter mPresenter;

    private LoadMoreRecyclerContainer mLoadMoreContainer;

    private ImageView mIvToTop;

    private String mCatId;

    private boolean isVisible;

    private boolean isPrepared;

    /**
     * @param isVisibleToUser 优先于onCreateView
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isVisible = true;
            onLazyLoad();
        }
    }

    private void onLazyLoad() {
        if (!isVisible || !isPrepared) {
            return;
        }
        mPresenter.start();
    }

    @Override
    protected boolean isMultiLoadData() {
        return true;
    }

    @Override
    protected void startLoadData() {
        if (getUserVisibleHint()) {
            onLazyLoad();
        }
    }

    public static DiscoveryActivePageFragment getInstance(String id) {
        DiscoveryActivePageFragment fragment = new DiscoveryActivePageFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Intents.DISCOVERY_CATID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initViews() {
        isPrepared = true;
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mPtrFrame = (PtrClassicFrameLayout) mRootView.findViewById(R.id.ptr_frame);
        mLoadMoreContainer = (LoadMoreRecyclerContainer) mRootView.findViewById(R.id.load_more_container);
        mIvToTop = (ImageView) mRootView.findViewById(R.id.iv_to_top);

        mCatId = getArguments().getString(Intents.DISCOVERY_CATID);

        initRecycleView();
        initListeners();

    }

    private void initRecycleView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new MultiTypeLoadMoreAdapter();
        mAdapter.register(TopArticle.class, new DiscoveryNewsTopPageProvider(getActivity()));
        mAdapter.register(Article.class, new DiscoveryActivePageProvider(getActivity()));
        mLoadMoreContainer.setRecyclerViewAdapter(mAdapter);
    }

    private void initListeners() {
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // 下拉刷新
                mPresenter.loadData(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }
        });

        mLoadMoreContainer.useDefaultFooter();
        mLoadMoreContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                // 加载更多
                mPresenter.loadData(false);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_discovery_secondary_page;
    }

    @Override
    public void setPresenter(DiscoveryActivePageContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }


    @Override
    public void setRefreshComplete() {
        mPtrFrame.refreshComplete();
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }

    @Override
    public String getCatId() {
        return mCatId;
    }

    @Override
    public void setPageData(List articleListBeen) {
        mAdapter.setItems(articleListBeen);
        mAdapter.notifyDataSetChanged();
    }
}
